<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="renderer" content="webkit" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>千锋教育大前端React教学</title>
  <!-- 引入react -->
  <script src="../../lib/js/react.js"></script>
  <!-- 引入react-dom -->
  <script src="../../lib/js/react-dom.js"></script>
  <!-- 引入babel 用来解析jsx -->
  <script src="../../lib/js/babel.js"></script>
</head>

<body style="margin:100px;font-size:22px;">
  <!-- 定义一个容器 -->
  <div id="app"></div>
  <script type="text/babel"> /*此处一定要写babel*/
    // 使用js类返回一个组件
    class Child extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          age: this.props.age, // React里面state的名称和props的名称可以一样
        };
        setTimeout(()=>{
          this.setState(state=>({ age: state.age + 1 }), ()=>{
            console.log('回调后的值：', this.state.age);
          });
          console.log('异步立即的值：', this.state.age);
        }, 2000);
      }
      render() {
        const { name } = this.props;
        const { age } = this.state;
        return <div>我叫{name}，我今年{age}岁了</div>;
      }
    }
    // 使用js函数返回一个组件
    function App() {
      return (
        <div>
          <Child name="孙权" age={8} />
        </div>
      );
    }
    // 挂载应用
    ReactDOM.createRoot(document.getElementById('app')).render(<App />);
  </script>
</body>

</html>